package isae.lib;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class InitialTreatments {

    Connection connection;

    public InitialTreatments() {
    }

    public String modiferMotPasse(String user, String pass, String pass1) throws ClassNotFoundException {
        String mess = "";
        Statement stmt = null;
        //int ResultSet = 0;
        try {
            connection = getJdbcDetail().getConnection();
            stmt = connection.createStatement();
            stmt.execute("BEGIN;");


            //   String userNew = "";
            //   String passNew = "";
            String pass1New = "";
            try {
                //      userNew = new String(user.getBytes("ISO-8859-1"), "UTF-8");
                //      passNew = new String(pass.getBytes("ISO-8859-1"), "UTF-8");
                pass1New = new String(pass1.getBytes("ISO-8859-1"), "UTF-8");
                //      user = userNew;
                //      pass = passNew;
                pass1 = pass1New;
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(InitialTreatments.class.getName()).log(Level.SEVERE, null, ex);
            }


            //ResultSet = stmt.executeUpdate("update Users set password='" + pass1 + "' where password=" + "\"" + pass + "\"" + "and username=" + "\"" + user + "\"");
            stmt.executeUpdate("update Users set password='" + pass1 + "' where password=" + "\"" + pass + "\"" + "and username=" + "\"" + user + "\"");

            stmt.execute("COMMIT;");
            connection.close();
            mess = "Votre mot de passe est modifié.";

        } catch (SQLException sqlException) {
            try {
                stmt.execute("ROLLBACK;");
                connection.close();
                mess = "La modification du mot de passe n'a pas eu lieu.";
            } catch (SQLException ex) {
                Logger.getLogger(InitialTreatments.class.getName()).log(Level.SEVERE, null, ex);
            }
            sqlException.printStackTrace();
        } catch (NamingException ex) {
            Logger.getLogger(InitialTreatments.class.getName()).log(Level.SEVERE, null, ex);
        }

        /*
        if (ResultSet == 1) {
        mess = "Votre mot de passe est modifié.";
        } else {
        mess = "La modification du mot de passe n'a pas eu lieu.";
        }
         */
        return mess;
    }

    public String modiferUsername(String user, String user1) throws ClassNotFoundException {
        String mess = "";
        Statement stmt = null;
        //int Result = 0;
        ResultSet records;
        try {
            connection = getJdbcDetail().getConnection();
            stmt = connection.createStatement();
            stmt.execute("BEGIN;");


            System.out.println("user:" + user);
            System.out.println("user1:" + user1);

            //  String userNew = "";
            String user1New = "";
            try {
                // userNew = new String(user.getBytes("ISO-8859-1"), "UTF-8");
                user1New = new String(user1.getBytes("ISO-8859-1"), "UTF-8");
                //   user = userNew;
                user1 = user1New;
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(InitialTreatments.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("usernew:" + user);
            System.out.println("user1new:" + user1);

            String requete = "select userId from Users where username=" + "\"" + user1 + "\"";
            records = stmt.executeQuery(requete);
            if (records.first() == true) {
                mess = "Votre nouveau nom d'utilisateur est déjà réservé, retapez un autre.";
            } else {
                stmt.executeUpdate("update Users set username='" + user1 + "' where username=" + "\"" + user + "\"");
                mess = "Votre nom d'utilisateur est modifié.";
            /* Result = stmt.executeUpdate("update Users set username='" + user1 + "' where username=" + "\"" + user + "\"");
            if (Result == 1) {
            mess = "Votre nom d'utilisateur est modifié.";
            } else {
            mess = "La modification du nom d'utilisateur n'a pas eu lieu.";
            }
             */
            }
            stmt.execute("COMMIT;");
            connection.close();


        } catch (SQLException sqlException) {
            try {
                stmt.execute("ROLLBACK;");
                connection.close();
                mess = "La modification du nom d'utilisateur n'a pas eu lieu.";
            } catch (SQLException ex) {
                Logger.getLogger(InitialTreatments.class.getName()).log(Level.SEVERE, null, ex);
            }
            sqlException.printStackTrace();
        } catch (NamingException ex) {
            Logger.getLogger(InitialTreatments.class.getName()).log(Level.SEVERE, null, ex);
        }
        return mess;
    }

    public String remplirMotsPasse() throws ClassNotFoundException {
        String mess = "ici";
        Statement stmt = null;
        Statement stmt1;
        ResultSet records;
        int no = 0;
        try {
            connection = getJdbcDetail().getConnection();
            stmt = connection.createStatement();

            stmt.execute("BEGIN;");
            String requete = "select UserId from Users where password is null or password=''";
            records = stmt.executeQuery(requete);

            while (records.next()) {
                no = records.getInt(1);
                InitialFunctions m = new InitialFunctions();
                String mot = m.getPassword(6);
                stmt1 = connection.createStatement();

                stmt1.executeUpdate("update Users set password=" + "\"" + mot + "\"" + " where UserId=" + no);
            }
            stmt.execute("COMMIT;");
            connection.close();
            mess = "l'opération a eu lieu avec succès.";
        } catch (SQLException sqlException) {
            try {
                stmt.execute("ROLLBACK;");
                connection.close();
                mess = "l'opération n'a pas eu lieu.";
            } catch (SQLException ex) {
                Logger.getLogger(InitialTreatments.class.getName()).log(Level.SEVERE, null, ex);
            }
            sqlException.printStackTrace();
        } catch (NamingException ex) {
            Logger.getLogger(InitialTreatments.class.getName()).log(Level.SEVERE, null, ex);
        }

        return mess;
    }

    public String remplirMotsPasseProf() throws ClassNotFoundException {
        String mess = "ici";
        Statement stmt = null;
        Statement stmt1;
        ResultSet records;
        int no = 0;
        try {
            connection = getJdbcDetail().getConnection();
            stmt = connection.createStatement();

            stmt.execute("BEGIN;");
            String requete = "select UserId from Users where (password is null or password='') and fonction="+"\"professeur\"";
            records = stmt.executeQuery(requete);

            while (records.next()) {
                no = records.getInt(1);
                InitialFunctions m = new InitialFunctions();
                String mot = m.getPassword(6);
                stmt1 = connection.createStatement();

                stmt1.executeUpdate("update Users set password=" + "\"" + mot + "\"" + " where UserId=" + no);
            }
            stmt.execute("COMMIT;");
            connection.close();
            mess = "l'opération a eu lieu avec succès.";
        } catch (SQLException sqlException) {
            try {
                stmt.execute("ROLLBACK;");
                connection.close();
                mess = "l'opération n'a pas eu lieu.";
            } catch (SQLException ex) {
                Logger.getLogger(InitialTreatments.class.getName()).log(Level.SEVERE, null, ex);
            }
            sqlException.printStackTrace();
        } catch (NamingException ex) {
            Logger.getLogger(InitialTreatments.class.getName()).log(Level.SEVERE, null, ex);
        }

        return mess;
    }

    private DataSource getJdbcDetail() throws NamingException {
        Context c = new InitialContext();
        return (DataSource) c.lookup("java:comp/env/jdbc/Detail");
    }
}